//
//  YSY_Game_NewsViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YSY_Game_NewsViewController.h"
#import "Masonry.h"
#import "YSY_Game_DocTableViewCell.h"
#import "GS_newsModel.h"
#import <MBProgressHUD.h>
#import "GoodManCollectionView.h"

@interface YSY_Game_NewsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *newsTableView;
@property (nonatomic, retain) NSMutableArray *newsArr;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) MBProgressHUD *hud;
@end

@implementation YSY_Game_NewsViewController

- (void)dealloc
{
    self.newsTableView.delegate = nil;
    self.newsTableView.dataSource = nil;
    
    [_newsArr release];
    [_newsTableView release];
    [_arr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.hud];
    self.hud.labelText = @"Loading";
    [self.hud show:YES];
    [self createView];
    [self getData];
}

#pragma mark - 私有方法
- (void)createView {
    [self createTabelView];
    
}
//创建UITabelView
- (void)createTabelView {
    
    
    self.newsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

    [self.view addSubview:self.newsTableView];
    [self.newsTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(30);
        
    }];
    
    self.newsTableView.delegate = self;
    self.newsTableView.dataSource = self;
    
}

#pragma  mark - UITableView协议方法
//Row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.newsArr) {
        return self.newsArr.count;
    }else {
        return 11;
    }
}


//铺cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    GS_newsModel *model = self.newsArr[indexPath.row];
    NSString *str = @"cell";
    YSY_Game_DocTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (nil == cell) {
        cell = [[YSY_Game_DocTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
//    cell.backgroundColor = [UIColor orangeColor];
    [cell.mainImageV sd_setImageWithURL:[NSURL URLWithString:model.cover_url] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    cell.titleL.text = model.title;
    cell.dateL.text = model.upload_time;
    return cell;
    
}
//头视图
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UIView *headerView = [UIView new];
//    headerView.backgroundColor = [UIColor cyanColor];
//    headerView.frame = self.newsTableView.tableHeaderView.bounds;
//    return headerView;
//}
////头视图高度
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return self.view.bounds.size.height / 4.0;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
    
}

- (void)getData
{
    self.newsArr = [NSMutableArray array];
    self.arr = [NSMutableArray array];
    [GoodManNews_AFNetTool getNetWithURL:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?tag=newest&p=1&action=l" body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        for (NSMutableDictionary *dic in responseObject) {
            
            GS_newsModel *model = [GS_newsModel baseModelWithDic:dic];
            [self.newsArr addObject:model];
            
        }
        
        [self.newsTableView reloadData];
        [self.hud hide:YES];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    NSString *str = @"http://lol.zhangyoubao.com/apis/rest/ItemsService/ads?&i_=97ECF1FF-26AA-4F7F-A7B6-854C625C2444&t_=1452254742&p_=16571&v_=40070101&d_=ios&osv_=8.3&version=0&a_=lol";
    
    [GoodManNews_AFNetTool getNetWithURL:str body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *dataArr = [responseObject objectForKey:@"data"];
        for (NSMutableDictionary *dic in dataArr) {
            NSString *str = [dic objectForKey:@"pic_ad_url"];
            [self.arr addObject:str];
            
        }
        self.newsTableView.tableHeaderView = [GoodManCollectionView collectionViewWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200) imageArray:self.arr timeInterval:1.5 view:nil];
        
        [self.newsTableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
